<template>
  <div class="quick-coupon-card">
    <!-- 微信支付优惠券-1 -->
    <div v-if="type == 1" class="type-1">
      <CouponCard :title="data.name"
                  :sub-title="data.deadline"
                  :water-text="data.status"
                  primary-bg-color="#FF8F00"
                  primary-bg-color2="#FFA000">
        <template v-slot:top-left>
          <div v-if="data.icon">
            <img :src="data.icon" style="width: 80px; height: 70px" alt="">
          </div>
          <FlexBox v-else center-h-v direction="column">
            <div>
              <span>{{ data.currentPrice }}</span>
              <span>{{ data.unit }}</span>
            </div>
            <div>{{ data.usage }}</div>
          </FlexBox>
        </template>
        <template v-slot:bottom>
          <div class="slot-bottom">
            <span class="primary-text">
              {{ data.convert }}
            </span>
            <span class="second-text">
              {{ data.desc }}
              <span class="strike">{{ data.originPrice + data.unit }}</span>
            </span>
          </div>
        </template>
      </CouponCard>
    </div>

    <!-- 微信支付优惠券-2 -->
    <div v-if="type == 2" class="type-2">
      <CouponCard :title="data.name"
                  :sub-title="data.deadline"
                  :water-text="data.status"
                  primary-bg-color="#00C250">
        <template v-slot:top-left>
          <div class="slot-top">
            <FlexBox direction="column" center-h-v class="slot-top-left">
              <FlexBox center-h-v>
                <div class="current-price-box">{{ data.currentPrice }}</div>
                <div class="unit">{{ data.unit }}</div>
              </FlexBox>
              <div class="usage">{{ data.usage }}</div>
            </FlexBox>
          </div>
        </template>
        <template v-slot:bottom>
          <div class="slot-bottom">
            <div class="primary-text">
              {{ data.toLock }}张待解锁
            </div>
            <span class="second-text">
              {{ data.desc }}
            </span>
          </div>
        </template>
      </CouponCard>
    </div>

    <!-- 微信支付优惠券-3 -->
    <div v-if="type == 3" class="type-3">
      <CouponCard :title="data.name"
                  title-color="#000"
                  :sub-title="data.deadline"
                  sub-title-color="rgba(0,0,0,.6)"
                  :water-text="data.status"
                  top-height="87px"
                  water-text-color="#999"
                  primary-bg-color="#FFF2EA">
        <template v-slot:top-left>
          <div class="slot-top">
            <FlexBox direction="column" center-h-v class="slot-top-left">
              <FlexBox center-h-v style="margin-top: -16px">
                <div class="current-price-box">{{ data.currentPrice }}</div>
                <div class="unit">{{ data.unit }}</div>
              </FlexBox>
              <div class="usage">{{ data.usage }}</div>
            </FlexBox>
          </div>
        </template>
        <template v-slot:bottom>
          <div class="slot-bottom">
            <span class="second-text">
              {{ data.desc }}
            </span>
          </div>
        </template>
      </CouponCard>
    </div>

    <!-- 首页弹窗获取优惠券-1 -->
    <div v-if="type == 4" class="type-4">
      <CouponCard :show-water-text="false"
                  diy-content
                  top-height="280px"
                  bottom-height="80px"
                  primary-bg-color="#FF8F00"
                  primary-bg-color2="#FFA000" style="margin: 0 auto">
        <template v-slot:diyContent>
          <div class="diy-coupon-top">
            <FlexBox center-h-v direction="column">
              <div class="title">微信支付</div>
              <div class="name font-ping-fang-bold">{{ data.name }}</div>
              <div class="desc-box">
                <div class="convert-box">
                  <span>{{handleConvert(data.convert, '=', 0)}}</span>=<span class="height-light">{{handleConvert(data.convert, '=', 1)}}</span>
                </div>
                <div class="divider"></div>
                <div class="desc">
                  {{ data.desc }}
                  <span class="strike">{{ data.originPrice + data.unit }}</span>
                </div>
              </div>
            </FlexBox>
          </div>
        </template>
        <template v-slot:bottom>
          <div class="slot-bottom">
            <div class="second-text">
              境外线下门店支付时，每单都可享受微信支付超优汇率，生效期7天
            </div>
          </div>
        </template>
      </CouponCard>
    </div>

    <!-- 首页弹窗获取优惠券-2 -->
    <div v-if="type == 5" class="type-5">
      <CouponCard :show-water-text="false"
                  diy-content
                  top-height="280px"
                  bottom-height="80px"
                  primary-bg-color="#00C250"
                  style="margin: 0 auto">
        <template v-slot:diyContent>
          <div class="diy-coupon-top">
            <FlexBox center-h-v direction="column">
              <div class="title">微信支付</div>
              <div class="name font-ping-fang-bold">{{ data.name }}</div>
              <!-- 中间的白色盒子 -->
              <div class="desc-box">
                <div class="center-h">
                  <div class="box">
                    <div class="inner-box">
                      <div class="text textAnimation font-ping-fang-bold" id="text">0</div>
                    </div>
                  </div>
                  <span style="margin-left: 5px">{{data.unit}}</span>
                </div>
                <!-- 5个小的优惠券icon -->
                <FlexBox class="icon-coupon-box" justify="space-between" align="center">
                  <IconCoupon class="box-item" direction="v" color="#DCDCDC" width-percent="44%" width="10px" height="14px" />
                  <IconCoupon class="box-item" direction="v" color="#1E8F4C" width-percent="60%" width="25px" height="25px">
                    <span>x2</span>
                  </IconCoupon>
                  <IconCoupon class="box-item" direction="v" color="#1E8F4C" width-percent="70%" width="30px" height="30px">
                    <span>x3</span>
                  </IconCoupon>
                  <IconCoupon class="box-item" direction="v" color="#1E8F4C" width-percent="80%" width="35px" height="35px">
                    <span>x4</span>
                  </IconCoupon>
                  <IconCoupon class="box-item" direction="v" color="#1E8F4C" width-percent="90%" width="40px" height="40px">
                    <img :src="require('../../assets/images/star.png')" style="width: 20px; height: 20px; opacity: 0.5" alt="">
                  </IconCoupon>
                </FlexBox>
                <div style="height: 20px"></div>

                <!-- 下面5个小圆点 -->
                <FlexBox class="icon-coupon-box dot-box" justify="space-between" align="center">
                  <FlexBox center-h-v style="position: relative;">
                    <div style="flex: 1; height: 2px"></div>
                    <FlexBox style="position: absolute;" center-h-v>
                      <div v-if="!stopped" class="dot"></div>
                      <img v-else class="icon-yes" :src="require('../../assets/images/yes.png')" alt="">
                    </FlexBox>
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                  </FlexBox>
                  <FlexBox center-h-v style="position: relative;">
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                    <FlexBox style="position: absolute;" center-h-v>
                      <div class="dot"></div>
                    </FlexBox>
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                  </FlexBox>
                  <FlexBox center-h-v style="position: relative;">
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                    <FlexBox style="position: absolute;" center-h-v>
                      <div class="dot"></div>
                    </FlexBox>
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                  </FlexBox>
                  <FlexBox center-h-v style="position: relative;">
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                    <FlexBox style="position: absolute;" center-h-v>
                      <div class="dot"></div>
                    </FlexBox>
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                  </FlexBox>
                  <FlexBox center-h-v style="position: relative;">
                    <div style="flex: 1; background-color: #0CB150; height: 2px"></div>
                    <FlexBox style="position: absolute;" center-h-v>
                      <div class="dot"></div>
                    </FlexBox>
                    <div style="flex: 1; height: 2px"></div>
                  </FlexBox>
                </FlexBox>
              </div>
            </FlexBox>
          </div>
        </template>
        <template v-slot:bottom>
          <div class="slot-bottom">
            <div class="second-text">
              使用后，扫不同礼包码，可获得2倍优惠，第5次将出现超级优惠
            </div>
          </div>
        </template>
      </CouponCard>
    </div>

    <!-- 商户优惠券 -->
    <div v-if="type == 6" class="type-6">
      <CouponCard diy-content :show-water-text="false" primary-bg-color="#FFF2EA" top-height="148px">
        <template v-slot:diyContent>
          <div class="diy-coupon-top">
            <FlexBox direction="column" center-h-v>
              <div class="title">{{ data.title }}</div>
              <div class="type">{{ data.name }}</div>
            </FlexBox>
            <FlexBox direction="column" center-h-v style="margin-top: -22px">
              <div v-if="data.icon" class="discount-box">
                <img :src="data.icon" alt="gift" class="icon">
              </div>
              <div v-else class="discount-box"><span
                  class="discount">{{ data.discount || data.currentPrice }}</span> <span
                  class="unit">{{ data.unit }}</span></div>

              <div v-if="data.usage" class="usage">{{ data.usage }}</div>
              <div v-else class="usage">原价 <span class="strike">{{ data.originPrice + data.unit }}</span></div>
            </FlexBox>
          </div>
        </template>
        <template v-slot:bottom class="">
          <flex-box center-h-v style="height: 100%">
            <Btn radius="4px" bg-color="#F15451" text-size="14px" style="padding: 6px 12px">使用优惠券</Btn>
          </flex-box>
        </template>
      </CouponCard>
    </div>
  </div>
</template>

<script>
import CouponCard from '@components/coupon-card/index.vue';
import FlexBox from '@components/flex-box/index.vue';
import Btn from '@components/btn/index.vue';
import IconCoupon from "@components/icon-coupon/index.vue";

export default {
  // 快速选择优惠券样式组件
  name: "QuickCouponCard",
  components: {IconCoupon, CouponCard, FlexBox, Btn},
  props: {
    type: {
      type: [Number, String],
      default: 1
    },
    data: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      stopped: false,
    }
  },
  computed: {

  },
  methods: {
    handleConvert(str='', ch='', index) {
      return str.split(ch)[index]
    },

    // 滚动选择数字
    scrollSelect(find) {
      let target = document.getElementById('text');
      if (target) {
        let i = 0;
        let count = 0;
        let interval = setInterval(() => {
          count++;
          target.innerText = i + '';
          i++;

          if (i === (find+1) && count > 20) {
            target.style.animationPlayState = 'paused'
            target.style.animation = null;
            target.classList.remove('textAnimation');
            clearInterval(interval);
            this.$emit('stopped');
            this.stopped = true;
          }
          if (i >= 10) i = 0;
        }, 100);
      }
    }
  },

  created() {

  },

  mounted() {
    window.requestAnimationFrame(() => {
      let find = Math.floor(Math.random() * 10);
      if (find === 0) find++;
      this.scrollSelect(find);
    });
  }

}
</script>

<style lang="scss" scoped>
.quick-coupon-card {
  width: 100%;

  .type-1 {
    .slot-bottom {
      font-size: 0.8rem;
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      padding-left: 20px;

      .primary-text {
        color: #111;
      }

      .second-text {
        margin-left: 10px;
        color: rgba(0, 0, 0, .3);
        font-weight: lighter;
        font-size: 0.7rem;
      }
    }
  }

  .type-2 {
    .slot-top {
      .slot-top-left {
        color: #fff;

        .current-price-box {
          background-color: rgba(0, 0, 0, .2);
          font-size: 40px;
          font-family: Helvetica;
          padding: 5px 10px;
          border-radius: 2px;
        }

        .unit {
          margin-top: 10px;
          margin-left: 4px;
        }

        .usage {
          opacity: .7;
          font-size: 12px;
          margin-top: 10px;
        }
      }
    }

    .slot-bottom {
      font-size: 0.8rem;
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      position: relative;
      &::after {
        content: "";
        position: absolute;
        width: 100%;
        bottom: 4px;
        border-bottom: 6px solid rgba(255,255,255, .8);
        border-bottom-left-radius: 2px;
        border-bottom-right-radius: 2px;
        box-shadow: 0px 1px 2px #ddd;
        z-index: 100;
      }
      &::before {
        content: "";
        position: absolute;
        width: 100%;
        bottom: 8px;
        border-bottom: 3px solid rgba(255,255,255, .8);
        border-bottom-left-radius: 2px;
        border-bottom-right-radius: 2px;
        box-shadow: 0px 1px 2px #ddd;
        z-index: 101;
      }

      .primary-text {
        font-size: 12px;
        color: rgba(0, 0, 0, .5);
        border: 1px solid rgba(0, 0, 0, .1);
        padding: 2px 8px;
        margin-left: 20px;
        border-radius: 999em;
      }

      .second-text {
        margin-left: 10px;
        color: rgba(0, 0, 0, .3);
        font-weight: lighter;
        font-size: 0.7rem;
      }
    }
  }

  .type-3 {
    .slot-top {
      .slot-top-left {
        color: #F15451;

        .current-price-box {
          font-size: 40px;
          font-family: Helvetica;
          border-radius: 2px;
          font-weight: 500;
        }

        .unit {
          margin-top: 10px;
          margin-left: 4px;
        }

        .usage {
          color: black;
          opacity: .6;
          font-size: 12px;
        }
      }
    }

    .slot-bottom {
      font-size: 0.8rem;
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      padding-left: 20px;

      .primary-text {
        color: #111;
      }

      .second-text {
        margin-left: 10px;
        color: rgba(0, 0, 0, .3);
        font-weight: lighter;
        font-size: 0.7rem;
      }
    }
  }

  .type-4 {
    width: 100%;

    .diy-coupon-top {
      width: 100%;
      color: #fff;
      line-height: 2;

      .title {
        margin-top: 44px;
        font-size: 16px;
      }

      .name {
        font-size: 21px;
      }
      .desc-box {
        width: 88%;
        background-color: #fff;
        padding: 20px;
        color: #000;
        line-height: 2.5;
        margin-top: 16px;
        border-radius: 3px;
        .convert-box {
          font-size: 13px;
          font-weight: bold;
          text-align: center;
          .height-light {
            color: #F15451;
            font-size: 20px;
          }
        }
        .divider {
          width: 100%;
          height: 1px;
          background-color: #888;
          transform: scaleY(0.4);
        }
        .desc {
          text-align: right;
          font-size: 12px;
          color: rgba(0,0,0, .6);
        }
      }
    }

    .slot-bottom {
      font-size: 0.8rem;
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      justify-content: center;
      padding: 25px;

      .primary-text {
        color: #111;
      }

      .second-text {
        color: rgba(0, 0, 0, .3);
        font-weight: lighter;
        font-size: 12px;
        line-height: 1.8;
      }
    }
  }

  .type-5 {
    width: 100%;

    .diy-coupon-top {
      width: 100%;
      color: #fff;
      line-height: 2;

      .title {
        margin-top: 30px;
        font-size: 16px;
      }

      .name {
        font-size: 18px;
      }
      .desc-box {
        width: 80%;
        padding: 20px 0;
        color: #fff;
        line-height: 2.5;
        margin-top: 16px;
        border-radius: 3px;
        .rect {
          padding: 4px 10px;
          background-color: rgba(0,0,0,.2);
          font-size: 20px;
        }

        .icon-coupon-box {
          position: relative;
          width: 100%;
          .box-item {
            flex: 1;
            align-self: flex-end;
            text-align: center;
            position: relative;
            color: #444;
            span {
              position: absolute;
              font-size: 80%;
              top: 0;
              bottom: 0;
              left: 0;
              right: 0;
            }
          }
          .dot-box {
            position: relative;
            width: 100%;
          }
          .dot {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background-color: #0CB150;
          }
          .icon-yes {
            width: 16px;
            height: 16px;
          }
        }

        .bar {
          position: absolute;
          width: 90%;
          height: 3px;
          background-color: rgba(66, 64, 64, 0.15);
          border-radius: 2px;
          bottom: 0;
          margin: 0 auto;
        }
      }
    }

    .slot-bottom {
      font-size: 0.8rem;
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      justify-content: center;
      padding: 25px;

      .primary-text {
        color: #111;
      }

      .second-text {
        color: rgba(0, 0, 0, .3);
        font-size: 12px;
        line-height: 1.8;
      }
    }

    .inner-box {
      width: 100%;
      height: 60px;
      background-color: rgba(0,0,0, .2);
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      color: #fff;
      position: relative;
      overflow: hidden;
      padding: 4px 6px;
      margin-top: -20px;
    }
    .text {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 30px;
      height: 80px;
      font-size: 46px;
      font-weight: 500;
    }
    .textAnimation {
      animation: scrollText 100ms infinite ease-in-out;
    }

    //数字滚动动画
    @keyframes scrollText {
      0% {transform: translateY(50%)}
      25% {transform: translateY(25%)}
      50% {transform: translateY(0%)}
      75% {transform: translateY(-25%)}
      100% {transform: translateY(-50%)}
    }
  }

  .type-6 {
    width: 100%;
    .diy-coupon-top {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: #F15451;

      .title, .type {
        color: black;
        font-weight: 500;
      }

      .discount-box {
        height: 50px;

        .discount {
          font-size: 40px;
          font-family: 黑体;
          font-weight: bold;
        }

        .icon {
          width: 40px;
          height: 40px;
        }
      }

      .unit {

      }

      .usage {
        font-size: 12px;
        opacity: 0.8;
      }
    }
  }
}
</style>
